<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          style="width: 99.9%"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :align="column.align" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list, remove } from '@/api/equipmentManage/sparePart/warehousing'
import { list as warehouseList } from '@/api/equipmentManage/sparePart/warehouse'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import Detail from './detail.vue'
import { getDictSelects } from '@/api/common/common'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '备件入库',
      pageExportId: '备件入库导出', // 页面导出标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      pageData2: {
        pageNum: 1,
        pageSize: 9999,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'formTypeLabel', label: '入库类型', canOrder: false },
        { prop: 'formCode', label: '入库单号' },
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称', canOrder: false },
        { prop: 'spareModel', label: '规格型号', canOrder: false },
        { prop: 'supplierName', label: '备件供应商', canOrder: false },
        { prop: 'spareType', label: '备件类型', canOrder: false },
        { prop: 'batch', label: '批号', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'priceThousandths', label: '备件单价(元)', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false, align: 'right' },
        { prop: 'number', label: '入库数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'deviceSpareWarehouseName', label: '仓库', canOrder: false },
        { prop: 'inByRealName', label: '入库人员', canOrder: false },
        { prop: 'inTime', label: '入库时间', minWidth: '140' },
        { prop: 'remark', label: '备注', canOrder: false }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'formTypeLabel', label: '入库类型', canOrder: false },
        { prop: 'formCode', label: '入库单号' },
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称', canOrder: false },
        { prop: 'spareModel', label: '规格型号', canOrder: false },
        { prop: 'supplierName', label: '备件供应商', canOrder: false },
        { prop: 'spareType', label: '备件类型', canOrder: false },
        { prop: 'batch', label: '批号', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'price', label: '备件单价(元)', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false, align: 'right' },
        { prop: 'number', label: '入库数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'deviceSpareWarehouseName', label: '仓库', canOrder: false },
        { prop: 'inByRealName', label: '入库人员', canOrder: false },
        { prop: 'inTime', label: '入库时间', minWidth: '140' },
        { prop: 'remark', label: '备注', canOrder: false }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        { prop: 'formTypeCode', label: '入库类型', type: 'dict', optionsName: 'dictOptions' },
        // { prop: 'formCode', label: '入库单号', type: 'input' },
        // { prop: 'spareCode', label: '备件编码', type: 'input' },
        // { prop: 'spareName', label: '备件名称', type: 'input' },
        // { prop: 'spareModel', label: '规格型号', type: 'input' },
        // { prop: 'supplierName', label: '备件供应商', type: 'input' },
        { prop: 'deviceSpareWarehouseId', label: '仓库', type: 'select', optionsName: 'warehouseOptions' },
        // { prop: 'inBy', label: '入库人员', type: 'input' },
        { prop: 'inTime', label: '入库时间', type: 'time', isTime: true }
        // { prop: 'spareType', label: '备件类型', type: 'input' },
        // { prop: 'batch', label: '批号', type: 'input' },
        // { prop: 'numberUnitLabel', label: '备件单位', type: 'input' }
      ],
      dictOptions: [], // 字典的数据源
      warehouseOptions: [],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'device:spareIn:add' },
        { label: '批量导出3', perm: 'device:spareIn:export' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: '1', label: '采购入库' },
        { value: '2', label: '外协入库' },
        { value: '3', label: '成品入库' }
      ],
      allTableData: []
    }
  },
  computed: {
  },
  created() {
    this.getDictOptions()
    this.getWarehouseList()
    this.getLists()
  },
  mounted() {

  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'formCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'formCode':
          this.see(row)
          break
      }
    },
    getExportData(data) {
      return list(data)
    },
    getDictOptions() {
      getDictSelects('SparePartsInStockFormType').then(res => {
        this.dictOptions = res.data
      })
    },
    getWarehouseList() {
      warehouseList().then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.name
        })
        res.data.unshift({ value: undefined, label: '全部' })
        this.warehouseOptions = res.data
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getLists() {
      list(this.pageData2).then(res => {
        this.allTableData = res.data
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['1320px', '700px'],
        title: '新增备件入库', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '700px'],
        title: '编辑备件入库', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    see(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: Detail, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '700px'],
        title: '查看备件入库', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
